package com.hd.trans.ui.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.hd.trans.R;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.ui.base.TransInit;
import com.hd.trans.utils.DeviceUtil;

public class SoundRecordingService extends Service {

    public static final int ID_MICROPHONE = 0xFC0001;
    public static final String CHANNEL_MICROPHONE = "channel_microphone";

    private static boolean mIsRunning = false;

    private static Runnable onStartCommandCallback = null;

    public static void start(Context context,@Nullable Runnable callback){
        Intent service = new Intent(context, SoundRecordingService.class);
        onStartCommandCallback = callback;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(service);
        }
        else {
            context.startService(service);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mIsRunning = true;
        showMicrophoneNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (onStartCommandCallback != null){
            onStartCommandCallback.run();
            onStartCommandCallback = null;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private void showMicrophoneNotification(){
        NotificationManager notificationManager = (NotificationManager) AppContextProvider.getContext().getSystemService(NOTIFICATION_SERVICE);

        Notification notification = createNotification(this);

        notificationManager.notify(ID_MICROPHONE, notification);
        startForeground(ID_MICROPHONE, notification);

    }

    public static Notification createNotification(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) AppContextProvider.getContext().getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(CHANNEL_MICROPHONE, context.getString(R.string.notification_channel), NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                CHANNEL_MICROPHONE)
                .setOngoing(true)
                .setSmallIcon(R.drawable.icon_microphone)
                .setLargeIcon(BitmapFactory.decodeResource(
                        context.getResources(), TransInit.getAppIconResourceId()))
                .setContentText(context.getString(R.string.notification_using))
                .setWhen(System.currentTimeMillis())
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentTitle(DeviceUtil.getAppName(AppContextProvider.getContext()));

        return builder.build();
    }

    public static boolean isRunning() {
        return mIsRunning;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onStartCommandCallback = null;
        mIsRunning = false;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

}
